package com.zyf.project.capchain.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.zyf.project.capchain.dto.UserDTO;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author Zhou Yifan
 */
@Data
public class User implements Serializable {

    /**
     * 用户id
     */
    private Long uid;

    /**
     * 小程序登录的 openid
     */
    private String openid;

    /**
     * 用户昵称
     */
    private String nickname;

    /**
     * 用户真实姓名
     */
    private String realname;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 用户的手机号
     */
    private String phoneNumber;

    /**
     * 用户登录密码
     */
    private String password;

    /**
     * 用户性别
     */
    private String gender;

    /**
     * 小程序登录的 session_key
     */
    private String sessionKey;

    /**
     * 用户token
     */
    private String token;

    /**
     * 是否为老用户
     */
    private Boolean isOld;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdTime;

    /**
     * 修改时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime modifiedTime;

    public User() {
        this.createdTime = LocalDateTime.now();
        this.modifiedTime = LocalDateTime.now();
    }

    public User(String openid, String sessionKey) {
        this.openid = openid;
        this.sessionKey = sessionKey;
        this.createdTime = LocalDateTime.now();
        this.modifiedTime = LocalDateTime.now();
    }

    /**
     * Model 转 DTO
     *
     * @return UserDTO
     */
    public UserDTO toDTO() {
        UserDTO userDTO = new UserDTO();
        userDTO.setUid(uid);
        userDTO.setOpenid(openid);
        userDTO.setNickname(nickname);
        userDTO.setRealname(realname);
        userDTO.setAvatar(avatar);
        userDTO.setPhoneNumber(phoneNumber);
        userDTO.setPassword(password);
        userDTO.setGender(gender);
        userDTO.setSessionKey(sessionKey);
        userDTO.setToken(token);
        userDTO.setIsOld(isOld);
        userDTO.setCreatedTime(createdTime);
        userDTO.setModifiedTime(modifiedTime);
        return userDTO;
    }

    /**
     * 判断对象是否逻辑为空
     *
     * @return 是否逻辑为空
     */
    public Boolean isLogicalEmpty() {
        return uid == null || openid.isEmpty();
    }

    /**
     * 判断对象认证逻辑是否为空，该方法调用前提是<b>用户已登录</b>
     *
     * @return 是否认证逻辑为空
     */
    public Boolean isAuthEmpty() {
        return openid.isEmpty() || token.isEmpty() || sessionKey.isEmpty();
    }

}
